﻿using System;
using System.Diagnostics;
using System.IO;
using System.Xml.Serialization;
using BluePortal.Medias.Core;

namespace BluePortal.Encoding.Core.ExpressionEncoder
{
    public class ExpressionEncoderRuntime
    {
        public string MediaEncoderPath { get; set; }

        private string GetJobFileTemplate()
        {
            return Utils.GetConfigFile("JobFile.xml");
        }

        private void UpdateConfig(JobFile jobfile, ExpressionEncoderOptions options)
        {
 
        }

        public string Encode(string sourceFile, Video video, string encodedVideoDirectory)
        {
            return Encode(sourceFile, video, encodedVideoDirectory, null);
        }

        public string Encode(string sourceFile, Video video, string encodedVideoDirectory, ExpressionEncoderOptions options)
        {
            string tmpjobFile = Path.Combine(encodedVideoDirectory, "jobfile.xml");

            // -- Create local dir to encode             
            if (!Directory.Exists(encodedVideoDirectory))
                Directory.CreateDirectory(encodedVideoDirectory);

            // -- Loading My Template job file
            XmlSerializer xs = new XmlSerializer(typeof(JobFile));
            JobFile jobfile = null;
            using (Stream configStream = File.OpenRead(GetJobFileTemplate()))
            {
                jobfile = (JobFile)xs.Deserialize(configStream);
                jobfile.Job[0].OutputDirectory = encodedVideoDirectory;
                UpdateConfig(jobfile, options);
                //Parse all MediaFiles from template
                foreach (MediaFile mf in jobfile.Job[0].MediaFiles)
                {
                    mf.Source = sourceFile;
                }                                
            }
            using (Stream configStream = File.OpenWrite(tmpjobFile))
            {
                xs.Serialize(configStream, jobfile);
            }

            // -- Create process start info
            FileInfo encoderInfo = new FileInfo(MediaEncoderPath);
            ProcessStartInfo psi = new ProcessStartInfo(encoderInfo.Name);
            psi.Arguments = "/JobFile \"" + tmpjobFile + "\"";
            psi.WorkingDirectory = encoderInfo.Directory.FullName;
            Process p = Process.Start(psi);

#if DEBUG
            Console.WriteLine(String.Format("{0} - Encoding > {1}", video.ID.ToString(), psi.FileName + " " + psi.Arguments));
#endif
            p.WaitForExit();
            
            //Read Other Format
            //@todo otherformats
            //for (int i = 1; i < jobfile.Job[0].MediaFiles.Length; i++)
            //{
            //    video.OtherVideoAttaches.Add(new OtherVideoAttach() { OVID = Guid.NewGuid(), ID = video.ID, Type = video.Type, FormatId = i });
            //}
            //Utils.ReadChapters(jobfile, tmpDir, video);

            return Path.Combine (encodedVideoDirectory, video.ID + ".wmv");
        }        
    }
}
